<?php


namespace addons\ymypaylinx\library\paylinx;


use fast\Random;
use think\Request;

class Wechat extends Base
{
    // 微信app支付链接
    protected $app_url = 'https://paylinx.cn/wxpay/gateway/app/';
    // 微信扫码支付链接
    protected $scan_url = 'https://paylinx.cn/wxpay/gateway/unifiedorder/';
    // H5支付链接
    protected $h5_url = 'https://paylinx.cn/wxpay/h5/?';
    // 汇率接口
    protected $exchagerate_url = 'https://paylinx.cn/wxpay/gateway/queryexchagerate/';
    // 订单状态
    protected $orderquery_url = 'https://paylinx.cn/wxpay/gateway/orderquery/';
    // 申请退款
    protected $refund_url = 'https://paylinx.cn/wxpay/gateway/refund/';
    // 查询退款
    protected $refund_find_url = 'https://paylinx.cn/wxpay/gateway/refundquery/';


    public function __construct(array $items = [])
    {
        parent::__construct($items);
    }

    // APP支付
    public function app($params){
        $params['trade_type'] = 'APP';
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->app_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result['requestParams'];
        }
        throw new \Exception("请求异常");
    }


    //扫码支付
    public function scan($params){
        $params['trade_type'] = 'NATIVE';
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->scan_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }

    //h5支付
    public function h5($params){
        $params['trade_type'] = 'JSAPI';
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->scan_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $this->h5_url.'mch_id='.$result['mch_id'].'&out_trade_no='.$result['out_trade_no'];
        }
        throw new \Exception("请求异常");
    }

    //查询汇率
    public function exchangeRate($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->exchagerate_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            return 5;
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            return 5;
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result['rate'];
        }
        return 5;
    }

    // 订单状态
    public function find($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->orderquery_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }


    //申请退款
    public function refund($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->refund_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }

    //查询退款
    public function findRefund($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->refund_find_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }




    //构建请求参数
    public function buildParams($params){
        if(isset($params['total_fee'])){
            $params['total_fee'] = $params['total_fee'] * 100;
        }
        $request = Request::instance();
        $baseData = [
            'mch_id' => $this->mch_id,
            'store_id' => $this->store_id,
            'appid' => $this->appid,
            'nonce_str' => Random::alnum(),
            'notify_url' => $this->notify_url,
            'body' => '商城支付',
            'fee_type'=>'AUD',
            'spbill_create_ip' => $request->ip()
        ];
        $data = array_merge($baseData,$params);
        $sign = $this->sign($data);
        $data['sign'] = $sign;
        $query = $this->arrayToXml($data);
        return $query;
    }
}